package com.guoxue.action.back;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.guoxue.action.front.BaseAction;
import com.guoxue.domain.BookConfigVo;
import com.guoxue.domain.PublishLogsVo;
import com.guoxue.domain.UpdateTableVo;
import com.guoxue.model.UpdateTable;
import com.guoxue.service.IBookConfigService;
import com.guoxue.service.IPublishLogsService;
import com.guoxue.service.IUpdateService;
import com.guoxue.util.JsonUtil;
import com.guoxue.util.LoadConfigProperties;
import com.guoxue.util.LoggerUtil;
import com.guoxue.util.OSSUtil;
import com.guoxue.util.RedisUtil;

import redis.clients.jedis.Jedis;
/**
 * 发布打包版本相关操作类
 * @author young
 */
@Controller
@RequestMapping("publish")
public class PublishAction extends BaseAction{
	
	@Resource
	private IPublishLogsService publishService;
	@Resource
	private IUpdateService updateService;
	@Resource
	private LoadConfigProperties load;
	@Resource
	private IBookConfigService bookService;
	@Resource
	private OSSUtil oss;
	/**
	 *  发布版本
	 * @param req
	 * @param resp
	 * @param versionCode
	 * @return
	 */
	@RequestMapping("/push")
	@ResponseBody
	public JSON push(HttpServletRequest req, HttpServletResponse resp, String versionCode){
		Jedis jedis = RedisUtil.getJedis();
		try {
			if (isEmpty(versionCode)){ 
				return Message(ERROR, "版本发布错误", null);
			} else {
				publishService.updateStatus(versionCode);
				load.valueInit();//加载静态资源
				jedis.set("checkVsersionCode", versionCode);
				return Message(SUCCESS, "版本发行成功", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"版本发布异常");
		} finally {
			jedis.close();
		}
	}
	/**
	 *  检查更新接口
	 * @param req
	 * @param resp
	 * @return
	 */
	@RequestMapping("/checkForUpdate")
	@ResponseBody
	public JSON checkForUpdate(HttpServletRequest req, HttpServletResponse resp){
		try {
			PublishLogsVo pl = publishService.getPublishByStatus("4");
			if (pl!=null){
				return Message(SUCCESS, "获取当前版本信息", pl);
			}else{
				return Message(ERROR, "获取版本信息失败", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"获取版本信息异常");
		}
	}
	/**
	 * 版本更新下载接口
	 * @param req
	 * @param resp
	 * @param versionCode
	 * @return
	 */
	@RequestMapping("/downloadForUpdate")
	@ResponseBody
	public JSON downloadForUpdate(HttpServletRequest req, HttpServletResponse resp, String versionCode){
		try {
			if (versionCode==null||"".equals(versionCode)){
				return Message(ERROR, "版本号不能为空", null);
			} else {
				List<UpdateTableVo> datas = updateService.getUpdateTableList(versionCode);
				if (datas.isEmpty()){
					return Message(ERROR, "获取更新数据空", null);
				} else {
					List<UpdateTable> ups = new ArrayList<UpdateTable>();
					for(UpdateTableVo up:datas){
						BookConfigVo book = bookService.getBookConfigById(up.getBookId());
						UpdateTable ta = new UpdateTable(up.getBookId(), up.getBookName(), up.getFileKey(), up.getFileKey2(), up.getId(), up.getKeyAndType(), up.getSize(), up.getVersionCode());
						ta.setPicX(isEmpty(book.getPicX())?null:oss.getUrl(bucket, book.getPicX(),  new Date(new Date().getTime() + 3600 * 1000 * 12)));
						ta.setPicXna(book.getPicX());
						ta.setPicY(isEmpty(book.getPicY())?null:oss.getUrl(bucket, book.getPicY(),  new Date(new Date().getTime() + 3600 * 1000 * 12)));
						ta.setPicYna(book.getPicY());
						ta.setPlanPic(isEmpty(book.getPlanPic())?null:oss.getUrl(bucket, book.getPlanPic(),  new Date(new Date().getTime() + 3600 * 1000 * 12)));
						ta.setPlanPicna(book.getPlanPic());
						ta.setReadPic(isEmpty(book.getReadPic())?null:oss.getUrl(bucket, book.getReadPic(),  new Date(new Date().getTime() + 3600 * 1000 * 12)));
						ta.setReadPicna(book.getReadPic());
						ta.setPicIcon(isEmpty(book.getPicIcon())?null:oss.getUrl(bucket, book.getPicIcon(),  new Date(new Date().getTime() + 3600 * 1000 * 12)));
						ta.setPicIconna(book.getPicIcon());
						ta.setPicTile(isEmpty(book.getPicTile())?null:oss.getUrl(bucket, book.getPicTile(),  new Date(new Date().getTime() + 3600 * 1000 * 12)));
						ta.setPicTilena(book.getPicTile());
						ups.add(ta);
					}
					return Message(SUCCESS, "获取成功", ups);
				}
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"获取版本信息异常");
		}
	}
	
}
